package com.zdsoft.crm.dao.auth;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zdsoft.crm.entity.auth.SysUser;


public interface ISysUserMapper {

	@Insert("insert into sys_user(id,username,password,is_admin)" 
	+ " values(#{id},#{username},#{password},#{is_admin,jdbcType=NUMERIC})")
	void save(SysUser user);

	@Select("select * from sys_user where username=#{username}")
	SysUser findByUserName(@Param("username")String username);
	
	@Select("select * from sys_user where id=#{id}")
	SysUser findById(String id);

	/*@Select("select id,username,create_time,is_admin from sys_user order by create_time desc limit #{startIndex},#{pageSize}  ")
	List<SysUser> pageList(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);
*/
	@Select("SELECT * FROM("
			+ "SELECT A.*,ROWNUM RN "
			+ "FROM "
			+ " (SELECT * FROM SYS_USER ) A"
			+ " WHERE ROWNUM<=#{endIndex})  TL "
			+ " WHERE RN > #{startIndex}")
	List<SysUser> pageList(@Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex);

	@Select("<script>"
			+ " select * from   (       "
			+ "  	select A.* ,ROWNUM rn"
			+ "		from ("
			+ "				select *   from SYS_USER"
			+ "            	<where>"
			+ "               	 	<if  test='username != null and username!=\"\"' >"
			+ "								AND username like '%'||#{username}||'%'"
			+ "					</if>"
			+ "            	</where> "
			+ "				) A"
			+ "		<if test='endIndex != null and endIndex!=\"\"' >"
			+ " 			<![CDATA[where ROWNUM <=#{endIndex}]]>"
			+ "		</if>  )"
			+ " <if test='beginIndex != null and beginIndex!=\"\"' >"
			+ " 		Where rn > #{beginIndex}"
			+ " </if>"
			+"</script>")
	List<SysUser> pageListWhere(@Param("beginIndex")int beginIndex, @Param("endIndex")int endIndex,@Param("username") String username);
	
	@Select("<script>SELECT count(id) FROM sys_user" + " where 1=1"
			+ " <if test='username != \"\"'> and username like '%'||#{username}||'%'</if></script>")
	Long totalCountWhere(@Param("username") String username);
	
	@Select("SELECT count(id) FROM sys_user")
	Long totalCount(String username);
	
	@Delete("delete from sys_user where id=#{id}")
	void deleteUser(@Param("id") String id);
	
	@Update("update sys_user set username=#{username},is_admin=#{is_admin,jdbcType=NUMERIC} where id=#{id}")
	void update(@Param("id") String id,@Param("username") String username,@Param("is_admin") Integer is_admin);
}
